<template>
  <q-layout>
    <q-layout-header ref="header">
      <xheader :title="title"></xheader>
    </q-layout-header>
    <q-page-container>
      <xscrollarea :scroll-height="scrollHeight">
        <q-page padding>
          <q-input ref="code" float-label="随工单号" v-model="code" clearable
                   @blur="searchCode"
                   @keydown.enter="searchCode" :after="$parent.getScanIcon('code',this)">
            <q-autocomplete
              @search="$parent.autoSearch"
              :min-characters="1"
              :max-results="99"
            />
          </q-input>
          <q-input float-label="随工单信息" placeholder="请输入随工单号" readonly v-model="codeInfo" type="textarea" rows="2"
                   hide-underline/>
          <q-select
            v-model="huban1"
            float-label="护板1"
            :options="hubanInfo" clearable
          />
          <q-select
            v-model="huban2"
            float-label="护板2"
            :options="hubanInfo" clearable
          />
          <q-select
            v-model="huban3"
            float-label="护板3"
            :options="hubanInfo" clearable
          />
          <q-select
            v-model="huban4"
            float-label="护板4"
            :options="hubanInfo" clearable
          />
          <q-select
            v-model="gaiban"
            float-label="盖板"
            :options="gaibanInfo" clearable
          />
          <br>
          <q-btn label="提交" color="primary" class="full-width" @click="submit"/>
        </q-page>
      </xscrollarea>
    </q-page-container>
  </q-layout>
</template>

<script>

import utils from '../../js/utils'
import 'lodash'
import Xscrollarea from '../../components/xscrollarea'
import Xheader from '../../components/xheader'

export default {
  name: 'manu_prepare',
  components: { Xheader, Xscrollarea },
  props: {
    title: {}
  },
  data: () => (
    {
      scrollHeight: 0,
      code: '',
      codeInfo: '',
      hubanInfo: [],
      huban1: '',
      huban2: '',
      huban3: '',
      huban4: '',
      gaibanInfo: [],
      gaiban: ''
    }),
  methods: {
    searchCode () {
      if (this.code !== '') {
        this.axios.post(this.$conf.server + '/manuPrepare/getCodeInfo', this.$qs.stringify({ code: this.code.replace('-A', '') })).then(res => {
          if (res.data.result) {
            this.codeInfo = '坩埚编码：' + utils.nvl(res.data.codeInfo.MAIN_CODE) +
                '\r\n配方名称：' + utils.nvl(res.data.codeInfo.PFNAME) + ' ' + utils.nvl(res.data.codeInfo.PFVERSION)
          } else {
            this.$parent.info(res.data.msg)
          }
        })
      }
    },
    initInfo () {
      this.axios.post(this.$conf.server + '/manuPrepare/getInitInfo', {}).then(res => {
        if (res.data.result) {
          for (let v of res.data.facListHB) {
            this.hubanInfo.push({ value: v.FACID, label: v.FACCODE })
          }
          for (let v of res.data.facListGB) {
            this.gaibanInfo.push({ value: v.FACID, label: v.FACCODE })
          }
          ;
        }
      }
      )
    },
    submit () {
      if (this.codeInfo === '') {
        this.$parent.info('随工单信息错误！')
        return
      }
      if (this.huban1 === '' || this.huban2 === '' || this.huban3 === '' || this.huban4 === '') {
        this.$parent.info('所有护板、盖板均不能为空！！')
        return
      }
      if (_.uniq([this.huban1, this.huban2, this.huban3, this.huban4]).length !== 4) {
        this.$parent.info('不同位置不能使用同一护板！')
        return
      }
      this.axios.post(this.$conf.server + '/manuPrepare/saveManuPrepare', this.$qs.stringify({
        manuBatchId: this.codeInfo.EMESO_MANU_BATCH_ID,
        huban1: this.huban1,
        huban2: this.huban2,
        huban3: this.huban3,
        huban4: this.huban4,
        gaiban: this.gaiban
      })).then(res => {
        this.$parent.info(res.data.msg)
        if (res.data.result) {
          this.code = ''
          this.codeInfo = ''
          this.huban1 = ''
          this.huban2 = ''
          this.huban3 = ''
          this.huban4 = ''
          this.gaiban = ''
        }
      })
    }
  },
  mounted () {
    this.scrollHeight = this.$parent.webHeight - this.$refs.header.$el.clientHeight
  },
  created () {
    this.initInfo()
  }
}
</script>

<style scoped>

</style>
